package com.gzc.just.play.last.war.common.share.communication;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Game event handler registry
 */
public class GameEventHandlerRegistry {
    
    private static final Map<String, List<GameEventHandler>> handlers = new ConcurrentHashMap<>();
    
    /**
     * Register event handler
     */
    public static void registerHandler(String eventType, GameEventHandler handler) {
        handlers.computeIfAbsent(eventType, k -> new CopyOnWriteArrayList<>()).add(handler);
    }
    
    /**
     * Get event handler list
     */
    public static List<GameEventHandler> getHandlers(String eventType) {
        return handlers.get(eventType);
    }
    
    /**
     * Remove event handler
     */
    public static void removeHandler(String eventType, GameEventHandler handler) {
        List<GameEventHandler> handlerList = handlers.get(eventType);
        if (handlerList != null) {
            handlerList.remove(handler);
            if (handlerList.isEmpty()) {
                handlers.remove(eventType);
            }
        }
    }
    
    /**
     * Clear all event handlers
     */
    public static void clearHandlers() {
        handlers.clear();
    }
}